var cookieList = [];
$(function () {
    start();
})

function getUrlVars() {
    var d = [], c;
    var a = window.location.href.slice(window.location.href.indexOf("?") + 1).split("&");
    for (var b = 0; b < a.length; b++) {
        c = a[b].split("=");
        d.push(c[0]);
        d[c[0]] = c[1]
    }
    return d
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function getUrlParameter(url, name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var result = url.match(/\?(.*)/);
    if (result) {
        var search = result[0]
        var r = search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
    }
    return null;
}

function start() {
    try {
        var arguments = getUrlVars();
        if (arguments.url === undefined) {
            // 使用新的API获取当前标签页
            chrome.tabs.query(
                {
                    active: true,
                    currentWindow: true
                },
                function (tabs) {
                    if (tabs && tabs.length > 0) {
                        var url = tabs[0].url;
                        var currentTabID = tabs[0].id;
                        document.getElementById("txtUrl").value = url;
                        document.title = document.title + "-" + url;
                        console.log(document.cookie);
                        doSearch();
                        setBaiduQueryString(url);
                    } else {
                        console.error('无法获取当前标签页');
                    }
                }
            );
        } else {
            var isSeparateWindow = true;
            var url = decodeURI(arguments.url);
            var currentTabID = parseInt(decodeURI(arguments.id));
            var isTabIncognito = decodeURI(arguments.incognito) === "true";
            document.getElementById("txtUrl").value = url;
            document.title = document.title + "-" + url;
            console.log(document.cookie);
            doSearch();
            setBaiduQueryString(url);
        }
    } catch (e) {
        console.log(JSON.stringify(e));
    }
}

function setBaiduQueryString(url) {
    if (url.indexOf("fengchao.baidu.com") > -1 || url.indexOf("tuiguang.baidu.com") > -1) {
        var userid = getUrlParameter(url, "userid");
        if (userid && userid.indexOf("#") > -1) {
            userid = userid.split("#")[0];
        }
        if (userid) {
            document.getElementById("txtSemID").value = userid;
            $("#sembox").show();
        }
    }
    else if (url.indexOf("tongji.baidu.com") > -1) {
        var userid = "未知";
        var result = url.match(/(.*)\?/);
        if (result) {
            var origin = result[0];
            var umatch = origin.match(/(\d+)/);
            if (umatch) {
                userid = umatch[0];
            }
        }
        document.getElementById("txtSemID").value = userid;
        $("#sembox").show();

        var siteId = getUrlParameter(url, "siteId");
        if (siteId) {
            document.getElementById("txtTongjiID").value = siteId;
            $("#tongjibox").show();
        }
    }
    else if (url.indexOf("lxb.baidu.com") > -1) {
        var userid = getUrlParameter(url, "userid");
        if (userid && userid.indexOf("#") > -1) {
            userid = userid.split("#")[0];
        }
        if (userid) {
            document.getElementById("txtSemID").value = userid;
            $("#sembox").show();
        }
    }
}

function doSearch() {
    var txt = document.getElementById("txtUrl").value;
    if (txt.length < 3)
        return;
    var filter = new Filter();
    if (/^https?:\/\/.+$/.test(txt)) {
        var url = txt;
        filter.setUrl(txt);
    } else {
        var url = 'http://' + txt;
        filter.setDomain(txt);
    }
    createList(filter.getFilter());
}

function createList(filters) {
    if (filters === null)
        filters = {};

    var filterURL = {};
    if (filters.url !== undefined)
        filterURL.url = filters.url;
    if (filters.domain !== undefined)
        filterURL.domain = filters.domain;

    var cookieArray = [];

    // 使用新的API获取cookies
    chrome.cookies.getAll(filterURL, function (cks) {
        console.log(cks);
        
        if (chrome.runtime.lastError) {
            console.error('获取cookies失败:', chrome.runtime.lastError);
            document.getElementById("txtCookie").value = "获取Cookie失败: " + chrome.runtime.lastError.message;
            return;
        }

        for (var i = 0; i < cks.length; i++) {
            var currentC = cks[i];
            cookieArray.push(currentC.name + "=" + currentC.value);
        }

        document.getElementById("txtCookie").value = cookieArray.join(";");
    });
}


